<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>countBy</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const countBy = (arr, handler) =>
            arr
                .map(typeof handler === 'function' ? handler : val => val[handler])
                .reduce((acc, item) => {
                    acc[item] = (acc[item] || 0) + 1;
                    return acc;
                }, {});
        console.log(countBy([6.1, 4.2, 6.3], Math.floor));
        console.log(countBy(['one', 'two', 'three'], 'length'));
        console.log(countBy([{ count: 5 }, { count: 10 }, { count: 5 }], x => x.count));
    </script>
</body>

</html>